﻿using System.Collections.Generic;
using Missbot.Common.Net;
using Missbot.Core.Cache;
using Missbot.WeiboBot.Model;
using Newtonsoft.Json;

namespace Missbot.WeiboBot
{
    /// <summary>
    /// 微博请求器
    /// </summary>
    public class WeiboRequestor
    {
        private long WeiboUID { get; }

        public WeiboRequestor(long WeiboUID)
        {
            this.WeiboUID = WeiboUID;
        }

        /// <summary>
        /// Cookie字典
        /// </summary>
        private CookieModel[] Cookies
        {
            get
            {
                var cache = GlobalVarDA.Get("WeiboBotCookie");
                return string.IsNullOrEmpty(cache?.Value) ? null : JsonConvert.DeserializeObject<CookieModel[]>(cache.Value);
            }
        }

        /// <summary>
        /// 获取微博列表
        /// </summary>
        /// <returns></returns>
        public WeiboListResp GetList()
        {
            const string url = "https://weibo.com/ajax/statuses/mymblog";
            var resp = HttpRequestor.GetData<WeiboListResp>(url, new Dictionary<string, string>
            {
                ["uid"] = WeiboUID.ToString(),
                ["page"] = "1",
                ["feature"] = "0"
            }, Cookies);
            return resp;
        }

        /// <summary>
        /// 获取长微博
        /// </summary>
        /// <param name="statusID"></param>
        /// <returns></returns>
        public WeiboLongResp GetLongText(string statusID)
        {
            const string url = "https://weibo.com/ajax/statuses/longtext";
            var resp = HttpRequestor.GetData<WeiboLongResp>(url, new Dictionary<string, string> { ["id"] = statusID }, Cookies);
            return resp;
        }

        /// <summary>
        /// 获取短微博
        /// </summary>
        /// <param name="statusID"></param>
        /// <returns></returns>
        public WeiboShortResp GetShortText(string statusID)
        {
            const string url = "https://weibo.com/ajax/statuses/show";
            var resp = HttpRequestor.GetData<WeiboShortResp>(url, new Dictionary<string, string> { ["id"] = statusID }, Cookies);
            return resp;
        }
    }
}